#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
using namespace std;

//#define READ_FILE

struct Wood
{
	short size;
	short weight;
	bool operator<=(Wood& w)
	{
		if (size != w.size) { return size <= w.size; }
		else { return weight <= w.weight; }
	}
	bool operator>(Wood& w)
	{
		if (size != w.size) { return size > w.size; }
		else { return weight > w.weight; }
	}
};
Wood g_wood[5000];

bool g_visited[5000];


/////////////////////////////
// quick sort
/////////////////////////////

int partition(int beg, int end)
{
	int pivotIndex = 1.0 * rand() / RAND_MAX * (end - beg);
	swap(g_wood[beg + pivotIndex], g_wood[beg]);
	
	Wood pivot = g_wood[beg];
	
	while (true)
	{
		while (g_wood[end] > pivot)
		{
			end--;
			if (beg == end) { return beg; }
		}
		
		
		swap(g_wood[beg], g_wood[end]);
		
		while (g_wood[beg] <= pivot)
		{
			beg++;
			if (beg == end) { return beg; }
		}
		
		swap(g_wood[beg], g_wood[end]);
	}
}

void quickSort(int beg, int end)
{
	if (beg < end)
	{
		int pivotIndex = partition(beg, end);
		quickSort(beg, pivotIndex-1);
		quickSort(pivotIndex+1, end);
	}
}



int calc(int woodCount)
{
	int count = 0;
	for (int i = 0; i < woodCount-1; ++i)
	{
		if (g_visited[i]) { continue; }
		g_visited[i] = true;
		
		count++;
		int last = i;
		for (int j = i+1; j < woodCount; ++j)
		{
			if (!g_visited[j] && g_wood[j].weight >= g_wood[last].weight)
			{
				g_visited[j] = true;
				last = j;
			}
		}
	}
	
	return count;
}




int main(int argc, char **argv)
{
#ifdef READ_FILE
	ifstream in("1025.input");
	if (!in)
	{
		in.open("../1025.input");
	}
#else
	#define in cin
#endif
	
	srand((unsigned)time(NULL));
	
	int caseCount; 
	in >> caseCount;
	while (caseCount)
	{
		--caseCount;
		
		memset(g_wood, 0, sizeof(g_wood));
		memset(g_visited, 0, sizeof(g_visited));
		int woodCount;
		in >> woodCount;
		for (int i = 0; i < woodCount; ++i)
		{
			in >> g_wood[i].size >> g_wood[i].weight;
		}
		
		quickSort(0, woodCount-1);
		int count = calc(woodCount);
		
		cout << count << endl;
	}
	
	
	return 0;
}
